#include"qplot.h"
#include<QDebug>


qplot::qplot(QWidget *parent)
{

    this->setParent(parent);
    tracer = new QCPItemTracer(this); //生成游标
    tracer->setPen(QPen(QColor(128,0,128,240)));//圆圈轮廓颜色
    tracer->setBrush(QBrush(QColor(128,0,128,240)));//圆圈圈内颜色
    tracer->setStyle(QCPItemTracer::tsCircle);//圆圈
    tracer->setSize(10);


    tracer_1 = new QCPItemTracer(this); //生成游标
    tracer_1->setPen(QPen(QColor("#2f4554")));//圆圈轮廓颜色
    tracer_1->setBrush(QBrush(QColor("#2f4554")));//圆圈圈内颜色
    tracer_1->setStyle(QCPItemTracer::tsCircle);//圆圈
    tracer_1->setSize(10);

    tracer_2 = new QCPItemTracer(this); //生成游标
    tracer_2->setPen(QPen(QColor("#61a0a8")));//圆圈轮廓颜色
    tracer_2->setBrush(QBrush(QColor("#61a0a8")));//圆圈圈内颜色
    tracer_2->setStyle(QCPItemTracer::tsCircle);//圆圈
    tracer_2->setSize(10);


    tracer_3 = new QCPItemTracer(this); //生成游标
    tracer_3->setPen(QPen(QColor("#d48265")));//圆圈轮廓颜色
    tracer_3->setBrush(QBrush(QColor("#d48265")));//圆圈圈内颜色
    tracer_3->setStyle(QCPItemTracer::tsCircle);//圆圈
    tracer_3->setSize(10);

    tracer_4 = new QCPItemTracer(this); //生成游标
    tracer_4->setPen(QPen(QColor(21, 88, 231)));//圆圈轮廓颜色
    tracer_4->setBrush(QBrush(QColor(21, 88, 231)));//圆圈圈内颜色
    tracer_4->setStyle(QCPItemTracer::tsCircle);//圆圈
    tracer_4->setSize(10);

    arrowH = new QCPItemStraightLine(this);
    arrowV = new QCPItemStraightLine(this);

    arrowH->setPen(QPen(QColor(190,190,190)));
    arrowV->setPen(QPen(QColor(190,190,190)));

    arrowH->setVisible(false);
    tracer->setVisible(false);
    tracer_1->setVisible(false);
    tracer_2->setVisible(false);
    tracer_3->setVisible(false);
    tracer_4->setVisible(false);
    arrowV->setVisible(false);


    this->setInteraction(QCP::iRangeDrag, true); //鼠标单击拖动

    this->yAxis->setLabelColor(QColor(150,150,150));//设置y轴标签颜色
    pen.setStyle(Qt::PenStyle::SolidLine);//虚线
    pen.setColor(QColor(190,190,190));
    this->yAxis->setTickPen(pen);

}

void qplot::leaveEvent(QEvent *event)
{
    QCustomPlot::leaveEvent(event);
    arrowH->setVisible(false);
    arrowV->setVisible(false);

    tracer->setVisible(false);
    tracer_1->setVisible(false);
    tracer_2->setVisible(false);
    tracer_3->setVisible(false);
    tracer_4->setVisible(false);

    this->replot();
}

void qplot::mouseMoveEvent(QMouseEvent *event)
{

    arrowH->setVisible(true);
    tracer->setVisible(true);
    tracer_1->setVisible(true);
    tracer_2->setVisible(true);
    tracer_3->setVisible(true);
    tracer_4->setVisible(true);
    arrowV->setVisible(true);
    float x = this->xAxis->pixelToCoord(event->pos().x());
    float y = this->yAxis2->pixelToCoord(event->pos().y());
    float y1 = this->yAxis->pixelToCoord(event->pos().y());

    tracer->setGraph(this->graph(0)); //将游标和该曲线图层想连接
    tracer->setGraphKey(x); //将游标横坐标设置成刚获得的横坐标数据x
    tracer->setInterpolating(true); //游标的纵坐标可以通过曲线数据线性插值自动获得
    tracer->updatePosition(); //使得刚设置游标的横纵坐标位置生效

    tracer_1->setGraph(this->graph(1)); //将游标和该曲线图层想连接
    tracer_1->setGraphKey(x); //将游标横坐标设置成刚获得的横坐标数据x
    tracer_1->setInterpolating(true); //游标的纵坐标可以通过曲线数据线性插值自动获得
    tracer_1->updatePosition(); //使得刚设置游标的横纵坐标位置生效

    tracer_2->setGraph(this->graph(2)); //将游标和该曲线图层想连接
    tracer_2->setGraphKey(x); //将游标横坐标设置成刚获得的横坐标数据x
    tracer_2->setInterpolating(true); //游标的纵坐标可以通过曲线数据线性插值自动获得
    tracer_2->updatePosition(); //使得刚设置游标的横纵坐标位置生效

    tracer_3->setGraph(this->graph(3)); //将游标和该曲线图层想连接
    tracer_3->setGraphKey(x); //将游标横坐标设置成刚获得的横坐标数据x
    tracer_3->setInterpolating(true); //游标的纵坐标可以通过曲线数据线性插值自动获得
    tracer_3->updatePosition(); //使得刚设置游标的横纵坐标位置生效

    tracer_4->setGraph(this->graph(4)); //将游标和该曲线图层想连接
    tracer_4->setGraphKey(x); //将游标横坐标设置成刚获得的横坐标数据x
    tracer_4->setInterpolating(true); //游标的纵坐标可以通过曲线数据线性插值自动获得
    tracer_4->updatePosition(); //使得刚设置游标的横纵坐标位置生效


    arrowH->point1->setCoords(1,y1);
    arrowH->point2->setCoords(2,y1);
    arrowV->point1->setCoords(x,2);
    arrowV->point2->setCoords(x,1);


    //"#c23531", "#2f4554", "#61a0a8", "#d48265"
    float yreal = tracer->position->value();
    if(qAbs(y-yreal)<=yreal/1000){
        pen.setWidth(3);
        pen.setColor(QColor("#c23531"));
    }
    else{
        pen.setWidth(1);
        pen.setColor(QColor("#c23531"));
    }

    this->graph(0)->setPen(pen);

    float yreal1 = tracer_1->position->value();
    if(qAbs(y-yreal1)<=yreal1/1000){
        pen.setWidth(3);
        pen.setColor(QColor("#2f4554"));
    }
    else{
        pen.setWidth(1);
        pen.setColor(QColor("#2f4554"));
    }

    this->graph(1)->setPen(pen);

    float yreal2 = tracer_2->position->value();
    if(qAbs(y-yreal2)<=yreal2/1000){
        pen.setWidth(3);
        pen.setColor(QColor("##61a0a8"));
    }
    else{
        pen.setWidth(1);
        pen.setColor(QColor("##61a0a8"));
    }

    this->graph(2)->setPen(pen);

    float yreal3 = tracer_3->position->value();
    if(qAbs(y-yreal3)<=yreal3/1000){
        pen.setWidth(3);
        pen.setColor(QColor("#d48265"));
    }
    else{
        pen.setWidth(1);
        pen.setColor(QColor("#d48265"));
    }

    this->graph(3)->setPen(pen);

    float yreal4 = tracer_4->position->value();
    if(qAbs(y-yreal4)<=yreal4/1000){
        pen.setWidth(3);
        pen.setColor(QColor(21, 88, 231));
    }
    else{
        pen.setWidth(1);
        pen.setColor(QColor(21, 88, 231));
    }

    this->graph(4)->setPen(pen);

    this->replot(); //重绘
    QCustomPlot::mouseMoveEvent(event);
}

void qplot::wheelEvent(QWheelEvent *event)
{


    double dCenter = this->xAxis->range().center();

    if(event->delta()<0&&this->xAxis->range().size()<100){

        this->xAxis->scaleRange(1.1, dCenter);
        this->replot();
    }
    else if(event->delta()>0&&this->xAxis->range().size()>9){

        this->xAxis->scaleRange(0.9, dCenter);
        this->replot();
    }
    QCustomPlot::wheelEvent(event);

}


